import { createRouter, createWebHistory } from 'vue-router'
import { isTokenExpired } from '@/utils/auth'
import Home from '../views/Home.vue'
import Login from '../views/auth/Login.vue'
import Register from '../views/auth/Register.vue'
import Live from '../views/Live.vue'
import Courses from '../views/Courses.vue'
import ClassIntro from '../views/ClassIntro.vue'
import CourseSeries from '../views/CourseSeries.vue'
import CoursePlayer from '../views/CoursePlayer.vue'
import StudentClasses from '../views/StudentClasses.vue'
import ClassDetail from '../views/ClassDetail.vue'
import Homework from '../views/homework/Homework.vue'
import Resources from '../views/Resources.vue'
import Study from '../views/Study.vue'
import About from '../views/about/About.vue'
import Careers from '../views/about/Careers.vue'
import Contact from '../views/about/Contact.vue'
import FAQ from '../views/help/FAQ.vue'
import Guide from '../views/help/Guide.vue'
import Feedback from '../views/help/Feedback.vue'
import Partnership from '../views/partnership/Partnership.vue'
import Enterprise from '../views/enterprise/Enterprise.vue'
import Instructor from '../views/instructor/Instructor.vue'
import Profile from '../views/Profile.vue'
import GradeHomework from '@/views/homework/GradeHomework.vue'
import ClassAnnouncements from '../views/ClassAnnouncements.vue'
import ClassKnowledgePoints from '../views/ClassKnowledgePoints.vue'
import ClassVideo from '../views/ClassVideo.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { 
      title: '登录 - 智能直播互动教育平台',
      hideNavigation: true 
    }
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: { 
      title: 'EduLiveLink - 智能直播互动教育平台'
    }
  },
  {
    path: '/home',
    redirect: '/'
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: { 
      title: '注册 - 智能直播互动教育平台',
      hideNavigation: true 
    }
  },
  {
    path: '/class/:classId/live',
    name: 'Live',
    component: Live,
    meta: { 
      title: '直播间 - 智能直播互动教育平台',
      requiresAuth: true
    }
  },
  {
    path: '/courses',
    name: 'Courses',
    component: Courses,
    meta: { 
      title: '精品课程 - 智能直播互动教育平台'
    }
  },
  {
    path: '/classes/:classId/intro',
    name: 'ClassIntro',
    component: ClassIntro,
    meta: {
      title: '班级介绍 - 智能直播互动教育平台'
    }
  },
  {
    path: '/courses/:courseId/series',
    name: 'CourseSeries',
    component: CourseSeries,
    meta: {
      title: '课程系列 - 智能直播互动教育平台'
    }
  },
  {
    path: '/courses/:courseId/preview/:lessonId',
    name: 'CoursePlayer',
    component: CoursePlayer,
    meta: {
      title: '课程预览 - 智能直播互动教育平台'
    }
  },
  {
    path: '/student-classes',
    name: 'StudentClasses',
    component: StudentClasses,
    meta: {
      title: '我的课程 - 智能直播互动教育平台',
      requiresAuth: true
    }
  },
  {
    path: '/class/:id',
    name: 'ClassDetail',
    component: ClassDetail,
    meta: {
      title: '班级详情 - 智能直播互动教育平台',
      requiresAuth: true
    }
  },
  {
    path: '/class/:id/homework',
    name: 'Homework',
    component: Homework,
    meta: {
      title: '交作业 - 智能直播互动教育平台',
      requiresAuth: true
    }
  },
  {
  	path: '/class/:classId/homework/grade',
  	name: 'GradeHomeworkList',
  	component: GradeHomework,
  	meta: {
  		title: '作业评价 - 智能直播互动教育平台',
  		isGrading: true,
  		requiresAuth: true
  	}
  },
  {
  	path: '/class/:classId/announcements',
  	name: 'ClassAnnouncements',
  	component: ClassAnnouncements,
  	meta: {
  		title: '班级公告 - 智能直播互动教育平台',
  		requiresAuth: true
  	}
  },
  {
  	path: '/class/:classId/knowledgepoints',
  	name: 'ClassKnowledgePoints',
  	component: ClassKnowledgePoints,
  	meta: {
  		title: '班级知识点 - 智能直播互动教育平台',
  		requiresAuth: true
  	}
  },
  {
  	path: '/class/:classId/videos',
  	name: 'ClassVideo',
  	component: ClassVideo,
  	meta: {
  		title: '发布视频'
  	}
  },
  {
    path: '/resources',
    name: 'Resources',
    component: Resources,
    meta: { 
      title: '学习资源 - 智能直播互动教育平台'
    }
  },
  {
    path: '/study',
    name: 'Study',
    component: Study,
    meta: { 
      title: '开始学习 - 智能直播互动教育平台',
      requiresAuth: true
    }
  },
  {
    path: '/about',
    name: 'About',
    component: About,
    meta: { 
      title: '关于我们 - 智能直播互动教育平台'
    }
  },
  {
    path: '/careers',
    name: 'Careers',
    component: Careers,
    meta: { 
      title: '加入我们 - 智能直播互动教育平台'
    }
  },
  {
    path: '/contact',
    name: 'Contact',
    component: Contact,
    meta: { 
      title: '联系我们 - 智能直播互动教育平台'
    }
  },
  {
    path: '/faq',
    name: 'FAQ',
    component: FAQ,
    meta: { 
      title: '常见问题 - 智能直播互动教育平台'
    }
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile,
    meta: { 
      title: '个人中心 - 智能直播互动教育平台',
      requiresAuth: true
    }
  },
  {
    path: '/guide',
    name: 'Guide',
    component: Guide,
    meta: { 
      title: '学习指南 - 智能直播互动教育平台'
    }
  },
  {
    path: '/feedback',
    name: 'Feedback',
    component: Feedback,
    meta: { 
      title: '意见反馈 - 智能直播互动教育平台'
    }
  },
  {
    path: '/partnership',
    name: 'Partnership',
    component: Partnership,
    meta: { 
      title: '机构合作 - 智能直播互动教育平台'
    }
  },
  {
    path: '/enterprise',
    name: 'Enterprise',
    component: Enterprise,
    meta: { 
      title: '企业培训 - 智能直播互动教育平台'
    }
  },
  {
    path: '/instructor',
    name: 'Instructor',
    component: Instructor,
    meta: { 
      title: '讲师招募 - 智能直播互动教育平台'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫 - 设置页面标题和权限控制
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }
  
  // 检查用户认证状态（包含过期校验）
  const token = localStorage.getItem('userToken') || sessionStorage.getItem('userToken')
  const expireAtStr = localStorage.getItem('userTokenExpireAt') || sessionStorage.getItem('userTokenExpireAt')
  const expireAt = expireAtStr ? Number(expireAtStr) : undefined
  const authenticated = !!token && !isTokenExpired(token, expireAt)
  
  console.log('路由守卫检查:', {
    to: to.path,
    isAuthenticated: authenticated,
    requiresAuth: to.meta.requiresAuth
  })
  
  // 如果访问登录或注册页面，直接通过
  if (to.path === '/login' || to.path === '/register') {
    console.log('访问认证页面，直接通过')
    next()
    return
  }
  
  // 如果用户未认证，重定向到登录页
  if (!authenticated) {
    console.log('未认证，重定向到登录页')
    next('/login')
    return
  }
  
  // 如果需要认证的页面且用户未登录
  if (to.meta.requiresAuth && !authenticated) {
    console.log('需要认证但未登录，重定向到登录页')
    next('/login')
    return
  }
  
  console.log('路由守卫通过，继续导航')
  next()
})

// 路由切换后滚动行为
router.afterEach((to, from) => {
  // 页面切换后立即滚动到顶部
  window.scrollTo(0, 0)
})

export default router
